﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using uAccess;
using WebApi.Models;

namespace KingCity.Service
{
    public class AdminUser
    {
        //显示所有用户
        public static JArray CustomersList()
        {
            JArray customers = new JArray();
            var op = new uAccess<Customers>("dbConnect").GetAll(); //当前op为列表形式
            /*
             JSON序列化:string JsonStr= JsonConvert.SerializeObject(Entity);（对象转化为字节流）
            JSON反序列化:Class model = JsonConvert.DeserializeObject<Class>(jsonstr);
             */
            var customers_str = JsonConvert.SerializeObject(op); 
            /*
             JArray将字符串转化为数组
            JObject将字符串转化为对象
             */
            customers = JArray.Parse(customers_str);
          
            return customers;
        }

        //显示某一个用户信息
        public static JObject GetUser(string C_Id)
        {
            var test = C_Id;
            // C_Id = C_Id.Substring(1, C_Id.Length - 2);//json传过来时会将"转义为"\，所以要进行字符截取

            uAccess<Customers> op = new uAccess<Customers>("dbConnect");
            //Services<Customers> op = new Services<Customers>("dbConnect");
            var pr = op.GetFirst(c => c.C_Id == C_Id);
            JObject userObj = new JObject
                {
                    {"C_Id",pr.C_Id},
                    {"C_Name",pr.C_Name},
                    {"C_Area",pr.C_Area},
                    {"C_Phone",pr.C_Phone},
                    {"C_AddressList",pr.C_AddressList},
                    {"C_Select",pr.C_Select},
                    {"C_Parent",pr.C_Parent},
                    {"C_Consumption",pr.C_Consumption},
                    {"C_Commission",pr.C_Commission},
                };

            return userObj;
        }

        //根据等级筛选部分用户
        public static JArray GetByRank(int C_Rank)
        {
            JArray customers = new JArray();
            var op = new uAccess<Customers>("dbConnect");
            var customers_str = "";
            if (C_Rank == 666)
            {
                customers_str = JsonConvert.SerializeObject(op.GetAll());
            }
            else
            {
                customers_str = JsonConvert.SerializeObject(op.GetAll(c=> c.C_Rank== C_Rank));
            }
            customers = JArray.Parse(customers_str);
            return customers;
        }

        //模糊查询
        public static JArray FuzzySearch(String SearchText)
        {
            JArray customers = new JArray();
            var op = new uAccess<Customers>("dbConnect"); //数据库连接对象
            var customers_str = JsonConvert.SerializeObject(op.GetAll(c => c.C_Id.Contains(SearchText)));
            customers = JArray.Parse(customers_str);
            return customers;
        }
    }
}